<?php
App::uses('AppController', 'Controller');
/**
 * GroupUsers Controller
 *
 * @property GroupUser $GroupUser
 */
class GroupUsersController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->GroupUser->recursive = 0;
		$this->set('groupUsers', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->GroupUser->exists($id)) {
			throw new NotFoundException(__('Invalid group user'));
		}
		$options = array('conditions' => array('GroupUser.' . $this->GroupUser->primaryKey => $id));
		$this->set('groupUser', $this->GroupUser->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$group_id=Cache::read('group_id');			
			if(empty($this->request->data['GroupUser']['user_id']))
			{
				Cache::delete('group_id');
				$this->Session->setFlash(__('No se ha seleccionado a ningún alumno para agregar, puede que no existan alumnos sin grupo.'));
				$this->redirect(array('controller'=>'groups','action' => 'view',$group_id));
			}
							
			$this->GroupUser->create();
			
			$users=$this->GroupUser->find('all',array('conditions'=>array('GroupUser.group_id'=>$group_id)));
			
			if(empty($users))
				$this->request->data['GroupUser']['lider']=1;	
			else
			{
				if($this->request->data['GroupUser']['lider']==1)
				{
					$this->GroupUser->query("update group_users set lider=0 where group_id=$group_id and lider=1");
				}
			}
			$this->request->data['GroupUser']['group_id']=$group_id;
			if ($this->GroupUser->save($this->request->data)) {
				//$this->Session->setFlash(__('The group user has been saved'));
				//$this->redirect(array('action' => 'index'));
				Cache::delete('group_id');
				$this->Session->setFlash(__('El alumno fue agregado con éxito al grupo.'));
				$this->redirect(array('controller'=>'groups','action' => 'view',$group_id));
			} else {
				$this->Session->setFlash(__('The group user could not be saved. Please, try again.'));
			}
		}
		$institution_id=Cache::read('group_institution_id');
		$options=array(
    'joins' => array(
        array(
            'table' => 'group_users',
            'alias' => 'GroupUser',
            'type' => 'Left',
            'conditions' => array(
                'User.id = GroupUser.user_id'
            )
        )),
	'conditions' =>array('GroupUser.user_id is null','User.role_id'=>3,'User.institution_id'=>$institution_id)
    );
		$groups = $this->GroupUser->Group->find('list');
		$users = $this->GroupUser->User->find('list',$options);
		//$institutions = $this->GroupUser->User->Institution->find('list',array('conditions' =>array('Institution.id'=>$institution_id)));
		$institutions = $this->GroupUser->User->Institution->find('list',array('conditions' =>array('Institution.id'=>$institution_id)));
		$this->set(compact('groups', 'users','institutions'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->GroupUser->exists($id)) {
			throw new NotFoundException(__('Invalid group user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->GroupUser->save($this->request->data)) {
				$this->Session->setFlash(__('The group user has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group user could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('GroupUser.' . $this->GroupUser->primaryKey => $id));
			$this->request->data = $this->GroupUser->find('first', $options);
		}
		$groups = $this->GroupUser->Group->find('list');
		$users = $this->GroupUser->User->find('list');
		$this->set(compact('groups', 'users'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null,$group_id,$lider) {
		$this->GroupUser->id = $id;
		if (!$this->GroupUser->exists()) {
			throw new NotFoundException(__('Invalid group user'));
		}
		$this->request->onlyAllow('post', 'delete');
		
		if ($this->GroupUser->delete()) {
			$this->Session->setFlash(__('El alumno fue eliminado del grupo'));
			if(!empty($lider))
				$this->GroupUser->query("update group_users set lider=1 where group_id=$group_id limit 1");
			$this->redirect("/groups/view/".$group_id);
		}
		$this->Session->setFlash(__('El alumno no se pudo eliminar del grupo.'));
		$this->redirect(array('action' => 'index'));
	}
}
